package com.hdu.seckilldemo.rabbitmq;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.nio.charset.StandardCharsets;

/**
 * RabbitMQ消息发送者
 */
@Service
@Slf4j
public class MQSender {
    //注入rabbitmq的消息模板
    @Autowired
    private RabbitTemplate rabbitTemplate;

    //RabbitMQ 完成异步下单
    public void send_Seckill_order(String msg){
        log.info("发送秒杀信息"+ msg);
        rabbitTemplate.convertAndSend("seckillExchange","seckill.order",msg);
    }
    //Fanout模式 给所有绑定的队列发送消息
    public void send(Object msg){
        log.info("message send:" + msg);
        rabbitTemplate.convertAndSend("fanoutExchange","",msg);
    }

    // direct模式下 给red发送消息
    public void send_red(Object msg){
        log.info("发送red消息:" + msg);
        rabbitTemplate.convertAndSend("directExchange","queue.red",msg);
    }

    // direct模式下 给green发送消息
    public void send_green(Object msg){
        log.info("发送green消息:" + msg);
        rabbitTemplate.convertAndSend("directExchange","queue.green",msg);
    }
    // topic模式下发送消息
    public void send_topic01(Object msg,String topic){
        log.info("发送消息("+topic+"):" + msg);
        rabbitTemplate.convertAndSend("headerEXCHANGE",topic,msg);
    }
    //header模式下发送消息
    public void send_header01(String msg){
        log.info("发送消息(被两个queue接收):" + msg);
        MessageProperties properties = new MessageProperties();
        properties.setHeader("color","red");
        properties.setHeader("speed","fast");
        Message message = new Message(msg.getBytes(),properties);
        rabbitTemplate.convertAndSend("headerEXCHANGE","",message);
    }

    public void send_header02(String msg){
        log.info("发送消息(被queue_topic01接收):" + msg);
        MessageProperties properties = new MessageProperties();
        properties.setHeader("color","red");
        properties.setHeader("speed","low");
        Message message = new Message(msg.getBytes(),properties);
        rabbitTemplate.convertAndSend("headerEXCHANGE","",message);
    }
}
